#include "junix.h"

int main(int argc, char **argv)
{
	if(argc != 2)
		err_quit("Usage:%s file",argv[0]);

	struct iovec vec;
	struct msghdr msg;
	struct cmsghdr *cmsg;
	int fd=passive_un("hello");
	int pfd=Open(argv[1], O_RDONLY);

	size_t len = strlen(argv[1])+1;
	char *pv=alloca(len);
	memcpy(pv, argv[1],len);
	vec.iov_base = pv;
	vec.iov_len=strlen(argv[1])+1;

	msg.msg_name=NULL;
	msg.msg_namelen=0;
	msg.msg_iov=&vec;
	msg.msg_iovlen=1;

	cmsg = alloca(sizeof(struct cmsghdr) + sizeof(int));
    cmsg->cmsg_len=sizeof(struct cmsghdr) + sizeof(int);
	cmsg->cmsg_level = SOL_SOCKET;
	cmsg->cmsg_type= SCM_RIGHTS;

	memcpy( CMSG_DATA(cmsg), &pfd, sizeof(pfd));
	msg.msg_control =cmsg;
	msg.msg_controllen =cmsg->cmsg_len;

	while(1)
	{
		int connfd=Accept(fd,NULL,NULL);
		if( sendmsg(connfd, &msg, 0) != vec.iov_len)
			err_sys( "sendmsg" );
		Lseek(pfd, 0, SEEK_SET);
		Close(connfd);
	}

	exit(0);
}
